package top.heue.text.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.preference.PreferenceManager;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import top.heue.text.ScrollingActivity;
import top.heue.text.sql.SQLite;

public class RunUtil {
    //检查是否第一次启动
    public static boolean isFirstRun(Context context) {
        SQLite sqLite = new SQLite(context);
        return !sqLite.isTableExits();
    }

    //获取内容界面文字拼音大小
    public static float getContentPinYinSize(Context context) {
        return Float.parseFloat(getPreference(context, "textSizePin", "7.0"));
    }

    public static float getContentWenZiSize(Context context) {
        return Float.parseFloat(getPreference(context, "textSizeWen", "20.0"));
    }


    //获取设置内容
    public static String getPreference(Context context, String key, String defaultValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(key, defaultValue);
    }

    //摘要显示字数格式化
    public static String contentFormat(Context context, String content) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        int i = Integer.parseInt(getPreference(context, "abstractNum", "50"));
        if (content.length() > i)//设置自定义项
        {
            content = content.substring(0, i);
        }
        return content;
    }

    //读取文件内容
    public static String read(String pathname) {
        String line2 = "";
        try {
            try (FileReader reader = new FileReader(pathname);
                 BufferedReader br = new BufferedReader(reader)
            ) {
                String line;
                while ((line = br.readLine()) != null) {
                    line2 += line + "\n";
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return line2;
    }

}
